﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace UDPClient2
{
    class Program
    {
        static void Main(string[] args)
        {
            UdpClient udpSender = new UdpClient(0);
            //连接到服务端并指定接收端口
            var p = new IPEndPoint(IPAddress.Loopback, 11000);
            udpSender.Connect(p);

            //把消息转换成字节流发送到服务端
            byte[] sendBytes = Encoding.UTF8.GetBytes("Hello World!");
            udpSender.Send(sendBytes, sendBytes.Length);
            var recieveBytes = udpSender.Receive(ref p);
            Console.WriteLine("返回:"+ Encoding.UTF8.GetString(recieveBytes));
            udpSender.Close();
            Console.ReadKey();
        }
    }
}
